package com.tom.architect02.mnbase.net;


import com.tom.architect02.p4.day37.retrofit.http.GET;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import retrofit2.http.Field;
import retrofit2.http.POST;

/**
 * Added by Tom on 2024/05/20.
 * 解析的类。封装到ServiceMethod，避免每次调用接口都反射执行耗时操作。
 */
public class ServiceMethod {

    Method method;
    private String methodName; // get，post
    Annotation[] methodAnnotations; // 方法注解，方法可能及支持get，又支持post
    private Map<String, Object> parameterMap = new HashMap<>();
    private String relativeUrl; // 绝对路径。


    Retrofit retrofit;
    Object[] args;
    Annotation[][] parameterAnnotationArray;

    public ServiceMethod(Retrofit retrofit, Method method, Object[] args) {
        this.retrofit = retrofit;
        this.method = method;
        this.args = args;
        this.methodAnnotations = method.getAnnotations();
        this.parameterAnnotationArray = method.getParameterAnnotations(); // 请求参数。

        build();
    }

    public ServiceMethod build() {
        // 做解析
        for (Annotation methodAnnotation : methodAnnotations) {
            if (methodAnnotation instanceof POST) { // 优先处理post
                methodName = "POST";
                this.relativeUrl = ((POST) methodAnnotation).value(); // 相对路径
                break;
            }
            if (methodAnnotation instanceof GET) {
                methodName = "GET";
                this.relativeUrl = ((GET) methodAnnotation).value();
                break;
            }
        }
        int parameterCount = parameterAnnotationArray.length;
        for (int p = 0; p < parameterCount; p++) {
            // 一个参数可能有多个注解。用数组承载。  拿到参数对象的注解数组集合
            Annotation[] parameterAnnotations = parameterAnnotationArray[p];
            Object value = args[p];
            for (Annotation parameterAnnotation : parameterAnnotations) {
                if (parameterAnnotation instanceof Field) {
                    // 拿到key
                    Field field = (Field) parameterAnnotation;
                    String key = field.value();
                    parameterMap.put(key, value);
                }
            }
        }
        return this;
    }

    public String getMethodName() {
        return methodName;
    }


    public String getBaseUrl() {
        // 拼接绝对路径+相对路径+请求参数
        if (methodName.equals("GET")) {
            StringBuffer sb = new StringBuffer();
            sb.append(retrofit.getBaseUrl()).append(relativeUrl);
            Map<String, Object> parameterMap = getParameter();

            if (parameterMap != null) {
                Set<String> keySet = parameterMap.keySet();
                if (keySet.size() > 0) {
                    sb.append("?");
                }
                for (String key : keySet) {
                    sb.append(key).append("=").append(parameterMap.get(key)).append("&");
                }
                sb.deleteCharAt(sb.length() - 1); // 去掉最后的 "&"
            }
            return sb.toString();
        }
        //        post请求不需要
        return retrofit.getBaseUrl();
    }

    public Map<String, Object> getParameter() {
        return parameterMap;
    }
}
